package com.zhouxiaoge.utils;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.session.Session;

import java.io.*;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-04-09 17:11
 */

public class SerializableUtils {

    public static String serialize(Session session) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(session);
            return Base64.encodeToString(outputStream.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException("deserialize session error", e);
        }
    }
    public static Session deserialize(String sessionStr) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
            ObjectInputStream ois = new ObjectInputStream(bis);
            return (Session)ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException("deserialize session error", e);
        }
    }
}
